package com.zj.recyclerviewdemo.view;

import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.Toast;

import com.zj.recyclerviewdemo.R;
import com.zj.recyclerviewdemo.adapter.MainAdapter;
import com.zj.recyclerviewdemo.base.BaseActivity;
import com.zj.recyclerviewdemo.bean.Item;
import com.zj.recyclerviewdemo.mvp.MainContract;
import com.zj.recyclerviewdemo.mvp.MainPresenter;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity
        implements MainContract.View, MainAdapter.OnItemClickListener,
        SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout swipeRefreshLayout;
    private RecyclerView recyclerView;

    private MainContract.Presenter presenter;
    private LinearLayoutManager layoutManager;
    private MainAdapter adapter;

    private List<Item> items;

    // 用于判断是否正在刷新
    private boolean isRefreshing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        new MainPresenter(this);
    }

    @Override
    public void setPresenter(MainContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void initViews() {
        swipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.refresh);
        swipeRefreshLayout.setOnRefreshListener(this);

        recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

        // use this setting to improve performance if you know that changes
        // in content do not change the layout size of the RecyclerView
        recyclerView.setHasFixedSize(true);

        layoutManager = new LinearLayoutManager(this);

        // use a linear layout manager
        recyclerView.setLayoutManager(layoutManager);

        items = new ArrayList<>();

        adapter = new MainAdapter(items);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);

        recyclerView.addItemDecoration(new DividerItemDecoration(
                this, DividerItemDecoration.VERTICAL));

        isRefreshing = false;
        recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                int lastVisibleItem = layoutManager.findLastVisibleItemPosition();
                int totalItemCount = layoutManager.getItemCount();
                if ((lastVisibleItem >= (totalItemCount - 2)) && dy > 0) {
                    if (!isRefreshing) {
                        isRefreshing = true;
                        swipeRefreshLayout.setRefreshing(isRefreshing);
                        presenter.getPullUpRefreshData();
                    }
                }
            }
        });

        presenter.getData();
    }

    @Override
    public void onLoading(String text) {

    }

    @Override
    public void loadingComplete() {

    }

    @Override
    public void loadingError(String error) {

    }

    @Override
    public void showData(List<Item> itemList) {
        if (itemList == null) {
            return;
        }

        items.addAll(itemList);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void showRefreshData(List<Item> itemList) {
        if (itemList == null) {
            return;
        }

        items.clear();
        items.addAll(itemList);
        adapter.notifyDataSetChanged();

        swipeRefreshLayout.setRefreshing(false);
    }

    @Override
    public void addRefreshData(List<Item> itemList) {
        if (itemList == null) {
            return;
        }

        items.addAll(itemList);
        adapter.notifyDataSetChanged();

        isRefreshing = false;
        swipeRefreshLayout.setRefreshing(isRefreshing);
    }

    @Override
    public void onItemClick(MainAdapter.ViewHolder viewHolder, int position) {
        Toast.makeText(this, "position = " + position, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onRefresh() {
        presenter.getRefreshData();
    }
}
